package org.rslai.autotest.core.driver.ie;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.rslai.autotest.core.driver.AbstractDriver;
import org.rslai.autotest.core.driver.RemoteWebDriverForAutoTest;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * IE远程驱动
 * @author rslai
 */
public class RemoteInternetExplorerDriver extends AbstractDriver {
	@Autowired
	private org.rslai.autotest.core.driver.ie.InternetExplorerProfile internetExplorerProfile; // InternetExplorerProfile

	@Override
	protected void startDriver() throws MalformedURLException  {
		DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
		internetExplorerProfile.setInternetExplorerProfile(capability);
		try {
			driver = new RemoteWebDriverForAutoTest(new URL(remoteAddress), capability);
		} catch (UnreachableBrowserException e) {
			UnreachableBrowserException exception = new UnreachableBrowserException("无法启动一个新会话。可能是无效的远程服务器的地址或浏览器启动失败。1、检查是否启动Selenium Server；2、检查防火墙设置。" + e.getMessage());
			exception.printStackTrace();
			throw exception;
		} catch (WebDriverException e) {
			WebDriverException exception = new WebDriverException("无法启动 IEDriverServer。1、检查IEDriverServer是否在系统path目录中；2、在环境变量中添加 webdriver.ie.driver；3、重新下载IEDriverServer。" + e.getMessage());
			exception.printStackTrace();
			throw exception;
		}

		driver.manage().timeouts().implicitlyWait(implicitlyWait, TimeUnit.SECONDS);
		if (maximize) {
			driver.manage().window().maximize();
		}
	}

}
